package handler

import (
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
	"videoProject/src/filemanage/api/internal/logic"
	"videoProject/src/filemanage/api/internal/svc"
	"videoProject/src/filemanage/api/internal/types"
)

func DownloadRequestHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.DownloadFileRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewDownloadRequestLogic(r.Context(), svcCtx)
		resp, err := l.DownloadRequest(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			//待优化 ，大文件下载不了
			w.Header().Set("Content-Disposition", "attachment; filename="+req.Name)
			w.Write(resp.Content)
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
